package com.forestar.mapControl.utils.mapConvert.geometry;


import android.icu.text.Transliterator;

import com.forestar.mapControl.utils.mapConvert.geometry.coordinatesystem.CoordinateSystem;
import com.forestar.mapControl.utils.mapConvert.geometry.coordinatesystem.ZoneType;
import com.forestar.mapControl.utils.mapConvert.geometry.utils.GeometryUtils;
import com.mapzone.api.geometry.mzPolygon;
import com.mapzone.api.geometry.mzRing;

import java.util.List;

public class GeoPolygon implements IGeometry
  {

    private CoordinateSystem coordSystem;
    private mzPolygon internalmzPolygon;

    public GeoPolygon(CoordinateSystem coordSystem)
      {
        this.internalmzPolygon = new mzPolygon();
        this.coordSystem = coordSystem;
        int nSrid = coordSystem.getSrid();
        this.internalmzPolygon.setSrid(nSrid);
      }

    public GeoPolygon(CoordinateSystem coordSystem, mzPolygon polygon)
      {
        this.internalmzPolygon = polygon;
        this.coordSystem = coordSystem;
        int nSrid = coordSystem.getSrid();
        this.internalmzPolygon.setSrid(nSrid);
      }

//    public GeoPolygon(CoordinateSystem coordSystem, Polygon jsonPolygon)
//      {
//        this.coordSystem = coordSystem;
//        //buildMemory();
//      }

    public boolean addPoints(List<GeoPoint> geoPoints)
      {
        GeoPoint pointReUse = new GeoPoint(coordSystem, 0.0, 0.0);
        ;
        int nPointsCount = geoPoints.size();
        mzRing m_mzRing = new mzRing();
        int nSrid = coordSystem.getSrid();
        m_mzRing.setSrid(nSrid);
        for (int i = 0; i < nPointsCount; i++)
          {
            GeoPoint point = geoPoints.get(i);
            CoordinateSystem.projectPoint(point, pointReUse);
            double dx = pointReUse.getX();
            double dy = pointReUse.getY();
            m_mzRing.AddPoint(dx, dy);
          }
        internalmzPolygon.AddRing(m_mzRing);
        return true;
      }

    private int id;

    public void setId(int id)
      {
        this.id = id;
      }

    public int getId()
      {
        return id;
      }

    public int getExteriorPointsCount()
      {
        return internalmzPolygon.GetExteriorRing().getPointCount();
      }

    /*public GeoPoint getExteriorPoint(int pointIndex)
      {
        mzRing ring = internalmzPolygon.GetExteriorRing()
        return ring.get
      }*/
    public int getRingCount()
      {
        return internalmzPolygon.getRingCount();
      }

    public int getPointsCount()
      {
        // mzRing ring = null;
        // ring = internalmzPolygon.GetExteriorRing();
        // return ring.getPointCount();
        return 0;
      }

    public List<Transliterator.Position> getPointList(int ringIndex)
      {
        return null;
      }

    public double getPointX(int ringIndex, int pointIndex)
      {
        mzRing ring = internalmzPolygon.GetExteriorRing();
        return ring.GetPoint(pointIndex).getX();
      }

    public double[] getPoints(int ringIndex)
      {
        return null;
      }

    public double getPointY(int ringIndex, int pointIndex)
      {
        mzRing ring = internalmzPolygon.GetExteriorRing();
        return ring.GetPoint(pointIndex).getY();
      }

    @Override
    public CoordinateSystem getCoordinateSystem()
      {
        return coordSystem;
      }

    @Override
    public GeometryType getGeometryType()
      {
        return GeometryType.GeometryTypePolygon;
      }

    @Override
    public Envelope getEnvelope()
      {
        return GeometryUtils.getEnvelope(internalmzPolygon);
      }

    @Override
    public void setCoordinateSystem(CoordinateSystem coordSystem)
      {
        this.coordSystem = coordSystem;
      }

    //没有投影的面积
    public double getArea()
      {
        return internalmzPolygon.getArea();
      }

    //投影后的面积:自动处理跨带问题
    public double getAreaEx(ZoneType zoneType)
      {
        //参数：nZone，0:3度分带加带号；1:3度分带不加带号；2:6度分带加带号，3:6度分带不加带号
        return internalmzPolygon.GetAreaEx(zoneType.getValue());
      }

    @Override
    public IGeometry Clone()
      {
        mzPolygon m_mzPolygonClone = (mzPolygon) this.internalmzPolygon.clone();
        return new GeoPolygon(this.coordSystem, m_mzPolygonClone);
      }

    @Override
    public byte[] getBlob()
      {
        return GeoConverter.Geom2Blob(this.internalmzPolygon);
      }

    @Override
    public boolean isSimple()
      {
        return internalmzPolygon.IsSimple();
      }

    @Override
    public Object getInternalObject()
      {
        return internalmzPolygon;
      }

    //获取所有环的长度和
    public double getLength()
      {
        return internalmzPolygon.getLength();
      }

    //获取所有环的长度和,如果有跨带环，则先分割，再投影到各自SRID下，再求长度和
    public double getLengthEx(ZoneType zoneType)
      {
        return internalmzPolygon.getLengthEx(zoneType.getValue());
      }

    // 取外环
    public mzRing GetExteriorRing()
      {
        mzRing ring = internalmzPolygon.GetExteriorRing();
        return ring;
      }

    // 获取内环个数
    public int getInnerRingCount()
      {
        return internalmzPolygon.getInnerRingCount();
      }

    // 取内环
    public mzRing GetInnerRing(int nIndex)
      {
        return internalmzPolygon.GetInnerRing(nIndex);
      }
  }
